<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>缺陷跟踪</el-breadcrumb-item>
      <el-breadcrumb-item>缺陷管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入搜索关键字"
            v-model="queryInfo.query"
            class="input-with-select"
            clearable
            @keyup.enter.native="getDefects"
            size="mini"
          >
            <el-button slot="append" icon="el-icon-search" @click="getDefects" size="mini"></el-button>
          </el-input>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" @click="exportExcel" size="mini">导出EXCEL</el-button>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" size="mini">下载EXCEL模板</el-button>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" size="mini">批量上传</el-button>
        </el-col>
        <el-col :span="3"><p></p></el-col>
        <el-col :span="3">
          <el-button type="primary" @click="addBtnClick" size="mini">新增缺陷</el-button>
        </el-col>
      </el-row>

      <!-- 缺陷表格区域 -->
      <el-table :data="defectList" border style="width: 100%" size="mini" stripe>
        <el-table-column type="index" label="序号" width="50"> </el-table-column>
        <el-table-column prop="outage" label="大修" width="60"> </el-table-column>
        <el-table-column prop="defect_order" label="缺陷通知单" width="110"> </el-table-column>
        <el-table-column prop="equipment" label="功能位置" width="150"> </el-table-column>
        <el-table-column prop="defect_detail" label="缺陷描述"> </el-table-column>
        <el-table-column prop="defect_date" label="缺陷日期" width="100"> </el-table-column>
        <el-table-column label="是否关闭" width="80">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.is_closed" @change="state_change(scope.row, '关闭')"> </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="closed_date" label="关闭日期" width="100"> </el-table-column>
        <el-table-column prop="team" label="发现专业" width="120"> </el-table-column>
        <el-table-column label="持续关注" width="80">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.is_concerned" @change="state_change(scope.row, '关注')"> </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="editBtnClick(scope.row.id)"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteDefectById(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页功能 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[20, 30, 50, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 20
      },
      total: 20,
      defectList: []
    }
  },
  created() {
    this.getDefects()
  },
  methods: {
    // 获取defect列表
    async getDefects() {
      const { data: res } = await this.$http.get('defect/getdefects/', { params: this.queryInfo })
      if (res.meta.status === 200) {
        this.defectList = res.data.page_defects
        this.total = res.data.total
        this.queryInfo.pagenum = res.data.pagenum
        return this.$message.success('查询缺陷数据成功')
      } else if (res.meta.status === 201) {
        this.queryInfo.pagenum = 1
        this.total = 0
        this.defectList = []
        return this.$message.info('未查询到相关数据')
      } else {
        this.queryInfo.pagenum = 1
        this.total = 0
        this.defectList = []
        return this.$message.error('获取缺陷列表失败')
      }
    },
    // 点击新增按钮的处理函数
    addBtnClick() {
      this.$router.push('/adddefect')
    },
    editBtnClick(defectId) {
      this.$router.push(`/editdefect?defectId=${defectId}`)
    },

    // 根据id删除defect信息
    async deleteDefectById(defectId) {
      const confirmResult = await this.$confirm('此操作将永久删除该缺陷, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      if (confirmResult === 'confirm') {
        const { data: res } = await this.$http.delete(`defect/deletedefect/${defectId}/`)
        if (res.meta.status !== 200) {
          return this.$message.error(res.meta.message)
        }
        this.$message.success(res.meta.message)
        this.getDefects()
      }
    },
    // 每页显示数量变化时的处理函数
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getDefects()
    },
    // 当前页面页码变化时的处理函数
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getDefects()
    },
    // 关注状态改变
    async state_change(defect, item) {
      const { data: res } = await this.$http.put(`defect/editdefect/${defect.id}/`, defect)
      if (res.meta.status !== 200) {
        return this.$message.error(`修改${item}状态失败！`)
      }
      this.$message.success(`修改${item}状态成功！`)
      this.getDefects()
    },
    // 导出缺陷的excel
    async exportExcel() {
      const { data: res } = await this.$http.get('defect/exportexcel/', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error('导出缺陷统计表失败！')
      }
      window.open(res.data.excel_url)
      this.$message.success('导出缺陷统计表成功！')
    }
  }
}
</script>

<style lang="less" scoped></style>
